cmake_minimum_required(VERSION 3.16)

project(nosal C CXX)

#set(NOSAL_TARGET_MACHINE QNX-SA8155)

# Use the currently recommended way to set a default value for CMAKE_INSTALL_PREFIX;
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "/tmp/nosal" CACHE STRING "Install location" FORCE)
endif()

# Allow to build the unit tests as well.
set(NOSAL_ENABLE_TESTS ON CACHE BOOL "Build the unit tests as well")

# This is for the nosal.cmake, which is needed by all our modules.
# We install it to this location, so let's make it accessible to all.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_INSTALL_PREFIX}/share/cmake-${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}/Modules)

# These are for the PkgConfig module
#list(APPEND CMAKE_PREFIX_PATH /usr/local/poco)
if(NOSAL_TARGET_MACHINE STREQUAL "QNX-SA8155")
else()
# These are for finding GTest and GMock
#set(GTEST_ROOT /usr/local/gtest)
#list(APPEND CMAKE_LIBRARY_PATH /usr/local/gtest/lib)
endif()

# CMake's usual "list" separation with ";" does not work when passing on to ExternalProject
string(REPLACE ";" "%" CMAKE_MODULE_PATH_FIXED "${CMAKE_MODULE_PATH}")
string(REPLACE ";" "%" CMAKE_PREFIX_PATH_FIXED "${CMAKE_PREFIX_PATH}")
string(REPLACE ";" "%" CMAKE_LIBRARY_PATH_FIXED "${CMAKE_LIBRARY_PATH}")

set(NOSAL_APP_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/opt")

message("Start configuring top level project : ${PROJECT_NAME}")
message("Generating using CMake version: " ${CMAKE_MAJOR_VERSION} "." ${CMAKE_MINOR_VERSION} "." ${CMAKE_PATCH_VERSION})

include(ExternalProject)
function(nosal_add_project NOSAL_NAME NOSAL_PATH)
    ExternalProject_Add(${NOSAL_NAME}
        SOURCE_DIR ${PROJECT_SOURCE_DIR}/${NOSAL_PATH}
        LIST_SEPARATOR %
        BUILD_ALWAYS 1
        CMAKE_ARGS
            --no-warn-unused-cli
            -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
            -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH_FIXED}
            -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH_FIXED}
            -DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH_FIXED}
            -DGTEST_ROOT=${GTEST_ROOT}
            -DNOSAL_ENABLE_TESTS=${NOSAL_ENABLE_TESTS}
            -DNOSAL_APP_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/opt
            -DNOSAL_TARGET_MACHINE=${NOSAL_TARGET_MACHINE}
    )
    if (${ARGC} GREATER 2)
        message("Adding dependencies: ${NOSAL_NAME} -> ${ARGN}")
        ExternalProject_Add_StepDependencies(${NOSAL_NAME} build ${ARGN})
    endif()
endfunction()

# Define "wrapper targets" for all our modules, including their dependencies.
nosal_add_project(nosal_cmake_modules            cmake_modules)

nosal_add_project(nosal_demo_lib                 demo/lib                  nosal_cmake_modules)

nosal_add_project(nosal_demo_app                 demo/app                  nosal_cmake_modules
                                                                           nosal_demo_lib)
nosal_add_project(nosal_demo_svc                 demo/svc                  nosal_cmake_modules
                                                                           nosal_demo_lib)
								   #nosal_add_project(nosal_demo_test                demo/test                 nosal_cmake_modules
								   #                                                                           nosal_demo_lib)
								   #nosal_add_project(nosal_core                     core                      nosal_cmake_modules)
								   #
								   #nosal_add_project(nosal_log                      log                       nosal_cmake_modules
								   #                                                                           nosal_core)
								   #nosal_add_project(nosal_exec                     exec                      nosal_cmake_modules
								   #                                                                           nosal_core
								   #                                                                           nosal_log)
								   #
